/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.client.screen.application;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.niiuzu.client.util.widget.LoadingBox;
import com.niiuzu.client.util.widget.error.BusinessErrorWidget;
import com.niiuzu.client.util.widget.error.TechnicalErrorWidget;
import com.niiuzu.client.util.widget.popup.PopupModal;

/**
 * Implémentation de la vue de l'application.
 */
public class ApplicationViewImpl extends Composite implements ApplicationView {

	/**
	 * Interface {@link UiBinder} adaptée à la vue {@link ApplicationViewImpl}.
	 */
	interface SuitedUiBinder extends UiBinder<Widget, ApplicationViewImpl> {

	}

	@UiField
	Panel mainPanel;
	@UiField
	Panel loadingApplicationPanel;
	@UiField
	Panel bodyPanel;
	@UiField
	Panel bodyContentPanel;
	@UiField
	Panel flagContainer;
	@UiField
	Panel logStatusPanel;
	@UiField
	SpanElement loggedInUserEmailAddress;
	@UiField
	Anchor logOutButton;

	private PopupModal loadingPopup;
	private LoadingBox loadingBox;

	@Override
	public void createView() {
		SuitedUiBinder uiBinder = GWT.create(SuitedUiBinder.class);
		initWidget(uiBinder.createAndBindUi(this));

		loadingBox = new LoadingBox();
		loadingPopup = new PopupModal(loadingBox);
	}

	@Override
	public void showLoadingApplication() {
		loadingApplicationPanel.setVisible(true);
	}

	@Override
	public void removeLoadingApplication() {
		mainPanel.remove(loadingApplicationPanel);
	}

	@Override
	public void showBodyPanel() {
		bodyPanel.setVisible(true);
	}

	@Override
	public void setBodyContent(IsWidget widget) {
		bodyContentPanel.clear();
		bodyContentPanel.add(widget);
	}

	@Override
	public void showPopupError(Throwable throwable) {
		TechnicalErrorWidget errorWidget = new TechnicalErrorWidget();
		errorWidget.setError(throwable);

		createAndShowPopup(errorWidget, errorWidget.getCloseButton());
	}

	@Override
	public void showPopupErrorMessage(String errorMessage) {
		BusinessErrorWidget errorWidget = new BusinessErrorWidget();
		errorWidget.setErrorMessage(errorMessage);

		createAndShowPopup(errorWidget, errorWidget.getCloseButton());
	}

	/**
	 * Crée et affiche une nouvelle popup.
	 * 
	 * @param bodyWidget
	 *            {@link Widget} constituant le corps de la popup.
	 * @param closeComponent
	 *            composant destiné à fermer la popup.
	 */
	private void createAndShowPopup(Widget bodyWidget,
			HasClickHandlers closeComponent) {
		PopupModal popup = new PopupModal(bodyWidget);
		popup.bindPopupCloser(closeComponent);
		popup.center();
	}

	@Override
	public void hideLoadingMessage() {
		loadingPopup.hide();
	}

	@Override
	public void showLoadingMessage(String message) {
		loadingBox.setHTML(message);
		loadingPopup.center();
	}

	@Override
	public void addFlagButton(ImageResource flagImage, String tooltip,
			ClickHandler handler) {
		Image image = new Image(flagImage);
		image.setTitle(tooltip);
		image.addClickHandler(handler);
		flagContainer.add(image);
	}

	@Override
	public HasClickHandlers getLogOutHasClickHandlers() {
		return logOutButton;
	}

	@Override
	public void hideLogStatusPanel() {
		logStatusPanel.setVisible(false);
	}

	@Override
	public void showLogStatusPanel(String emailAddress) {
		loggedInUserEmailAddress.setInnerText(emailAddress);
		logStatusPanel.setVisible(true);
	}
}
